package com.nervenets.web.controller.handler.admin.setting;

import com.nervenets.utils.JodaUtils;
import com.nervenets.web.controller.Global;
import com.nervenets.web.controller.handler.SessionHandler;
import com.nervenets.web.controller.handler.common.IdTokenParams;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.enumeration.Role;
import com.nervenets.web.hibernate.domain.setting.Tags;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

public class TagDeleteHandler extends SessionHandler<IdTokenParams> {
    @Override
    public ArrayList<Role> allowRoles() {
        return Role.administrator();
    }

    @Override
    public boolean needLog() {
        return false;
    }

    @Override
    public String handle(IdTokenParams params, HttpServletRequest request) throws Exception {
        Tags tags = userDao.findById(params.getTargetId(), Tags.class);
        if (null == tags) return errorMessage(MessageCode.code_404, "没有找到修改的标签");
        tags.setRemoved(JodaUtils.getTimestamp());
        userDao.saveOrUpdate(tags);
        globalService.setGlobalPair(Global.Constants.TAGS_KEY, "");
        return successMessage();
    }

    @Override
    public String result() throws Exception {
        return null;
    }
}
